package com.ctbri.vSearch.service.impl;

import com.ctbri.vSearch.frame.RedisPooledConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

import javax.annotation.Resource;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Service;

import com.ctbri.vSearch.bean.dto.ArticleAndTermScore;
import com.ctbri.vSearch.bean.dto.SearchItem;
import com.ctbri.vSearch.bean.po.ArticleItem;
import com.ctbri.vSearch.bean.po.GzItem;
import com.ctbri.vSearch.dao.es.ScoreModel;
import com.ctbri.vSearch.dao.neo4j.Neo4jQueryDao;
import com.ctbri.vSearch.dao.neo4j.RelationMapQurey;
import com.ctbri.vSearch.service.DeepSearchServer;
import redis.clients.jedis.Jedis;


@Service("deepSearchServerImpl")
public class DeepSearchServerImpl implements DeepSearchServer{

	@Resource
	Neo4jQueryDao neo4jQuery;
	@Resource
	RelationMapQurey relationMapQuery;
	@Resource
	ScoreModel scoreModel;
	
	@Override
	public void searchRelationMap(CountDownLatch countdownlatch,
			String word, List<String> nodes,List<String> links,	ObjectMapper mapper, 
			String[] category, Jedis client) {
		relationMapQuery.queryRelationMap(neo4jQuery, countdownlatch,word, nodes, 
				links,mapper, category, client);
	}
	
	@Override
	public Map<String, Double> searchRelatedWords( String word) {
		return  relationMapQuery.queryRelationWords( word);
	}
	
	@Override
	public void searchScoreModel(Map<String,Double> internalResults, ArticleAndTermScore articleAndTermScore ,RedisPooledConnection rpc) {
		scoreModel.scoreModel(internalResults,articleAndTermScore,rpc);
	}

	@Override
	public void dereplicationGzs() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dereplicationArticles() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dereplicationGzsAndArticles(List<SearchItem> searchItems,
			List<GzItem> gzItems,ArticleAndTermScore articleAndTermScore) {
/*		// 文章对应的打分
		Map<ArticleItem, Double> articleScoremap = scoreModel.getArticleScore();
		// 文章对应每个搜索词的打分
		Map<ArticleItem, HashMap<String, Float>> term = scoreModel.getTermScore();
		*/
		Map<ArticleItem, Double> articleScoremap =articleAndTermScore.getArticleScore();
		Map<ArticleItem, HashMap<String, Float>> term = articleAndTermScore.getTermScore();
		for(Map.Entry<ArticleItem, Double> entry :  articleScoremap.entrySet()){//遍历所有文章
			ArticleItem article = entry.getKey();
			Double score = entry.getValue();//得到文章的评分
			SearchItem tmpSItem = new SearchItem();
			tmpSItem.setTitle(article.getTitle());
			tmpSItem.setContent(article.getContent());
			tmpSItem.setTmp(term.get(article).keySet());
			tmpSItem.setScore(score);
			tmpSItem.setUrl(article.getUrl());
			if(!searchItems.contains(tmpSItem)){
				searchItems.add(tmpSItem);
			}
			
			String GzName = article.getOffcial_account();
			if(GzName !=null){
				GzItem tmpGzItem = new GzItem();
				tmpGzItem.setName(GzName);
				if(!gzItems.contains(tmpGzItem)){
					gzItems.add(tmpGzItem);
				}
			}
		}
		
	}



	


}
